﻿package cn.valorin.util;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import static cn.valorin.configuration.languagefile.MessageSender.sm;


public class EatingCommands {
    public static void ExecuteCommands(Player p,String log_string)
    {
	  String way;
	  try {
	    way = log_string.split("\\|")[0].replace("command:", "");
	  } catch (Exception e) {
		sm("&c执行command动作时发生错误，请截图给腐竹让腐竹检查插件的配置文件中Action项的格式是否有误",p);
		return;
	  }
	  String command;
	  try {
		command = log_string.split("\\|")[1];
	  } catch (Exception e) {
		sm("&c执行command动作时发生错误，请截图给腐竹让腐竹检查插件的配置文件中Action项的格式是否有误",p);
		return;
	  }
	  if (way.equalsIgnoreCase("op"))
	  {
	    if (p.isOp())
	    {
	      Bukkit.dispatchCommand(p, command.replace("<player>", p.getName()).replace("&", "§"));
	    } else {
	      p.setOp(true);
	      Bukkit.dispatchCommand(p, command.replace("<player>", p.getName()).replace("&", "§"));
	      p.setOp(false);
	    }
	  }
	  if (way.equalsIgnoreCase("player"))
	  {
	    Bukkit.dispatchCommand(p, command.replace("<player>", p.getName()).replace("&", "§"));
	  }
	  if (way.equalsIgnoreCase("console"))
	  {
	    Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command.replace("<player>", p.getName()).replace("&", "§"));
	  }
    }
}
